<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *场地管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$serve_id=intval($_RQ['id']);
	if($serve_id){
		$this->session('serve_id',$serve_id);
	}else{
		$serve_id=$this->session('serve_id');
	}
	if(!$serve_id){
		exi('请先选择项目！','error');
	}
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['store_id']=$store_id;
	$params['serve_id']=$serve_id;
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','title','status','price','paixu');
	$list=pdo_getslice('jy_qiuyuyue_serve_plate',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'场地列表'),
			array('op'=>'edit','title'=>'添加场地'),
		),
		'list'=>array('data'=>'list','width'=>'15','tip'=>'这里设置场馆项目的不同场地，比如：场地1，场地2!','edit'=>true,'list'=>array(
			array('type'=>'string','title'=>'排序','name'=>'paixu'),
			array('name'=>'title','title'=>'场地名称','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'场地名称','type'=>'string','width'=>'50'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_qiuyuyue_serve_plate',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['item']['serve_time']=$return['item']['serve_time'] ? json_decode($return['item']['serve_time'],true):array();
	$return['item']['time_box']=$return['item']['time_box']?json_decode($return['item']['time_box'],true):array();
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回场地列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
						array(
							'title'=>'基本信息',
							'list'=>array(
								array('type'=>'string','title'=>'场地名称','name'=>'title','help'=>'','ismust'=>true),
								array('type'=>'string','title'=>'排序','name'=>'paixu','help'=>'填写数字，按从小到大排序展示'),
								array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>'设置是否启用'),
								//array('type'=>'checkbox','title'=>'场次时间','name'=>'work_time','help'=>'','options'=>'work_time'),
								/*array(
									'type'=>'time_box',
									'name'=>'serve_time',
									'title'=>'场次时间段',
									'add_tip'=>'添加一个时间段',
									'title1'=>'时间段',			
									'field1'=>'times',
									'title2'=>'价格',
									'field2'=>'fee',
									'field2unit'=>'元',
								),*/
							)
						),
			
			)
	);
	$return['work_time']=array('1'=>'周一','2'=>'周二','3'=>'周三','4'=>'周四','5'=>'周五','6'=>'周六','7'=>'周日');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$serve_id=$this->session('serve_id');
	$data=array(
		'title'=>trim($postdata['title']),
		'price'=>intval($postdata['price']),
		'work_time'=>trim($postdata['work_time']),
		'serve_time'=>ijson_encode($postdata['serve_time']),
		'status'=>intval($postdata['status']),
		'paixu'=>intval($postdata['paixu']),
		'store_id'=>$store_id,
		'serve_id'=>$serve_id,
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_qiuyuyue_serve_plate',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_qiuyuyue_serve_plate',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_qiuyuyue_serve_plate',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的场地！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_qiuyuyue_serve_plate',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['ishot'])){
		pdo_update('jy_qiuyuyue_serve_plate',array('ishot'=>intval($post['ishot'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}